package com.hj.security.engine;

import java.util.ArrayList;
import java.util.List;

import com.hj.security.domain.AppInfo;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

public class AppInfoProvider {

	private PackageManager packageManager;

	public AppInfoProvider(Context context) {
		//拿到一个包管理器
		this.packageManager = context.getPackageManager();
	}
	
	public List<AppInfo> getAllApps(){
		List<AppInfo> list = new ArrayList<AppInfo>();
		AppInfo appInfo;
		//获取到所有安装了的应用程序的信息，包括那些卸载了的，但没有清除数据的应用程序
		List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
		for (PackageInfo packageInfo : packageInfos) {
			appInfo = new AppInfo();
			//拿到包名
			String packageName = packageInfo.packageName;
			//拿到应用程序的信息
			ApplicationInfo applicationInfo = packageInfo.applicationInfo;
			//拿到应用程序的图标
			Drawable icon = applicationInfo.loadIcon(packageManager);
			//拿到应用程序的程序名
			String appName = applicationInfo.loadLabel(packageManager).toString();
			appInfo.setPackageName(packageName);
			appInfo.setIcon(icon);
			appInfo.setAppName(appName);
			if(filterApp(applicationInfo)){
				appInfo.setSystemApp(true);
			}else{
				appInfo.setSystemApp(false);
			}
			list.add(appInfo);
		}
		return list;
	}
	/**
	 * 判断一个应用程序是不是系统的应用程序，如果是返回true,否则返回false
	 * @param applicationInfo
	 * @return
	 */
	public boolean filterApp(ApplicationInfo applicationInfo){
		if((applicationInfo.flags&applicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0){
			return true;
		}else if((applicationInfo.flags&applicationInfo.FLAG_SYSTEM)!=0){
			return true;
		}
		return false;
	}
}
